//
//  ChatHController.m
//  GuShiNews
//
//  Created by huachun on 2019/8/28.
//  Copyright © 2019 Jones. All rights reserved.
//

#import "ChatHController.h"
#import "ChatListCell.h"
//#import "ChatViewModel.h"
#import "ChatModel.h"
//#import "ChatAction.h"
#import "ChatSql.h"
#import "HiChatInfoController.h"
#import "RootSuperAction.h"

@interface ChatHController (){
    ChatSql        *sqliteletter;
    NSMutableArray *Toparray;
}


@end

@implementation ChatHController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.leftBarButtonItems = nil;
    [self setnavback];
    
    self.Data_array = [NSMutableArray new];
    
//    ChatViewModel *viewmodel = [[ChatViewModel alloc] init:@[]];
//    Toparray = [viewmodel.dataSource mutableCopy];
//    [Toparray removeObjectAtIndex:0];
    
    sqliteletter= [[ChatSql alloc] init];
    
    [self load_new_chat];
    
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(new_chat:)
                                                 name:NEW_CHATLIST_Key
                                               object:nil];
    
    [self.view addSubview:self.TableView];
    self.TableView.separatorInset      = UIEdgeInsetsMake(0, 80, 0, 0);
    self.TableView.separatorStyle      = UITableViewCellSeparatorStyleSingleLineEtched;
    self.TableView.backgroundColor = [JonesClass stringTOColor:@"#fbfbfb"];
    self.TableView.separatorColor  = [JonesClass stringTOColor:@"#e9e9e9"];
}

- (NSAttributedString *)buttonTitleForEmptyDataSet:(UIScrollView *)scrollView forState:(UIControlState)state {
    NSString *text;
    NSMutableAttributedString *attStr;
    if ([UserModel info].userid) {
        text = @"您还没收到私信";
    }else{
        text = @"请登录";
    }
    attStr = [[NSMutableAttributedString alloc] initWithString:text];
    [attStr addAttribute:NSFontAttributeName
                   value:[UIFont systemFontOfSize:16.0]
                   range:NSMakeRange(0, text.length)];
    [attStr addAttribute:NSForegroundColorAttributeName
                   value:[JonesClass stringTOColor:@"#cccccc"]
                   range:NSMakeRange(0, text.length)];
    return attStr;
}

#pragma mark - DZNEmptyDataSetDelegate
- (void)emptyDataSet:(UIScrollView *)scrollView didTapButton:(UIButton *)button {
    if (![UserModel info].userid) {
        [RootSuperAction Actiontodone:JonesGOTO_Login view:self dictionary:@{}];
    }
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self load_new_chat];
}

- (void)load_new_chat{
    NSMutableArray *lin_list = [sqliteletter selectdata];
    [self.Data_array removeAllObjects];
    [self.Data_array addObjectsFromArray:lin_list];
    
    [self.TableView reloadData];
}

-(void)new_chat:(NSNotification *)notification{
//    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
//        [self load_new_chat];
//    });
    [self load_new_chat];
}

#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.Data_array.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 80;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ChatListCell *cell = [ChatListCell cellForTableView:tableView cellForRowAtIndexPath:indexPath];
    cell.Model = [self.Data_array objectAtIndex:indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    ChatModel *model = [self.Data_array objectAtIndex:indexPath.row];
    
    UserModel *modeluser = [UserModel new];
    modeluser.userid = model.User_id;
    modeluser.name = model.User_name;
    modeluser.logo = model.User_logo;
    modeluser.sex = model.sex;
    modeluser.briday = model.briday;
    
    HiChatInfoController *chatvc = [HiChatInfoController new];
    chatvc.hidesBottomBarWhenPushed = YES;
    chatvc.Model = modeluser;
    [self.navigationController pushViewController:chatvc animated:YES];
    
    
//    [ChatAction Actiontodone:HiGOTO_Chat_AddNews view:self dictionary:@{@"username":model.User_name,@"userlogo":model.User_logo,@"userid":model.User_id}];
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        ChatModel *Model=[self.Data_array objectAtIndex:indexPath.row];
        if (self.Data_array.count == 1) {
            [sqliteletter deletedata:Model];
            [self.Data_array removeObjectAtIndex:indexPath.row];
            [self.TableView reloadData];
        }else{
            [sqliteletter deletedata:Model];
            [self.Data_array removeObjectAtIndex:indexPath.row];
            [self.TableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
        }
    }
    else if (editingStyle == UITableViewCellEditingStyleInsert) {}
}

@end
